﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using AllEntity;

namespace MyWCFServer
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MyService : IMyService
    {
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        public Person GetSignlePerson()
        {
            Person person = new Person { name = "abc", age = "11", sex = "男" };
            return person;
        }

        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        public List<Person> GetAllPerson()
        {
            List<Person> list = new List<Person>();
            for (int i = 1; i < 6; i++)
            {
                Person person = new Person();
                person.name = "Name:[" + i + "]";
                person.age = "Age :[" + i + "]";
                person.sex = "Sex :[" + i + "]";

                list.Add(person);
            }

            return list;
        }

        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        public string GetPersonAgeByName(string name)
        {
            int age = new Random().Next(1, 100);
            if (name != "")
            {
                return age.ToString();
            }
            throw new Exception("姓名不能为空");
        }

        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        public bool InsertPerson(Person person)
        {
            if (person != null)
            {
                return true;
            }
            return false;
        }
    }
}
